package dao.impl;

import bean.Bean1;
import dao.mapper.Bean1Mapper;
import wrap.WrapBean1;
import dao.Bean1DAO;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Bean1DAOImpl implements Bean1DAO {
    private SqlSession session;
    private Bean1Mapper mapper;
    private boolean status;

    public Bean1DAOImpl(SqlSessionFactory factory) {
        session = factory.openSession();
        mapper = session.getMapper(Bean1Mapper.class);
    }

    @Override
    public boolean add(WrapBean1 wb) {
        try {
            status = mapper.ins(wb);
            session.commit();
        } catch (Exception e) {
            status = false;
            e.printStackTrace();
        }
        return status;
    }

    @Override
    public boolean mod(Bean1 bean) {
        try {
            status = mapper.upd(bean);
            session.commit();
        } catch (Exception e) {
            status = false;
            e.printStackTrace();
        }
        return status;
    }

    @Override
    public boolean rem(int f1) {
        try {
            status = mapper.del(f1);
            session.commit();
        } catch (Exception e) {
            status = false;
            e.printStackTrace();
        }
        return status;
    }

    @Override
    public Bean1 que(int f1) {
        Bean1 bean;
        try {
            bean = mapper.a(f1);
        } catch (Exception e) {
            bean = null;
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public List<Bean1> que(Bean1 bean) {
        List<Bean1> list;
        try {
            list = mapper.b(bean);
        } catch (Exception e) {
            list = null;
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Bean1> que(WrapBean1 wb) {
        List<Bean1> list;
        try {
            list = mapper.c(wb);
        } catch (Exception e) {
            list = null;
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Bean1> que(int[] arr) {
        Map<String, int[]> map = new Hashtable<>();
        map.put("arr", arr);
        List<Bean1> list;
        try {
            list = mapper.d(map);
        } catch (Exception e) {
            list = null;
            e.printStackTrace();
        }
        return list;
    }
}
